// pages/select/index.js
import api from '../../utils/api'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectQuestionMenu: '请选择',
    objectQuestionMenu: {},
    questionMenu: [],
    index: 0,
    show:false,
    show2:false,
    answer:'',
    type:'',
    id:'',
    is_pay:0
  },

  onShow() {
    console.log('onShow监听小程序显示');
    let _this = this;
    let token = wx.getStorageSync('token');
    if(token!=null && token!=''){
      api.get("index?token="+token, {
      }).then(res => {
        console.log(res)
        if(res.data.length > 0){
            _this.setData({
              questionMenu:res.data,
              is_pay:res.data[0].is_pay
            })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }) 
    }else{
      this.setData({
        show2:true
      })
      // Dialog.alert({
      //   message: '请先前往个人中心登录方可生成评测'
      // }).then(() => {
      //   // on close
      //   wx.switchTab({
      //     url: '/pages/my/my',
      //   })
      // });
    }
    
  },
  gotoMy(){
    wx.switchTab({
      url: '/pages/my/my',
    })
  },
  /**
   * 选择题库
   */
  changeMenu(e) {
    console.log(this.data.questionMenu[e.detail.value]);
    this.setData({
      index: e.detail.value,
      selectQuestionMenu: this.data.questionMenu[e.detail.value]
    })
  },
// 生成订单
  creatOrder(){
    console.log('生成订单号')
    let _this = this;
    let token = wx.getStorageSync('token');
      api.post("evaluation/order", {
        "token":token,
      }).then(res => {
        console.log(res)
        _this.pay(res.data)
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }) 
  },
  // 获取支付信息
  pay(order_no){
    console.log('根据订单号获取支付信息')
    let _this = this;
    let token = wx.getStorageSync('token');
      api.post("evaluation/wxpay", {
        "token":token,
        order_no:order_no,
      }).then(res => {
        console.log(res)
        _this.wxPay(res.data.timeStamp.toString(),res.data.nonceStr,res.data.package,res.data.paySign)
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }) 
  },
  onClose() {
    this.setData({ close: false });
  },

  /**
   * 开始答题
   */
  startAnswer(e) {
    console.log(e)
    let answer = e.target.dataset.answer;
    let id = e.target.dataset.id;
    let type= e.target.dataset.type
    this.setData({
      show:true,
      answer:answer,
      id:id,
      type:type
    })
  },
   /**
   * 微信自带支付
   */
  wxPay:function(t,n,p,pn){
    let _this = this;
    wx.requestPayment(
      {
      'timeStamp': t,
      'nonceStr': n,
      'package': p,
      'signType': 'MD5',
      'paySign': pn,
      'success':function(res){
        wx.showToast({
          title:"支付成功",
          icon:"none"
        })
        _this.gotoAnswerWxPay()
      },
      'fail':function(res){
        console.log(res)
        wx.showToast({
          title:"支付失败",
          icon:"none"
        })
      },
      'complete':function(res){
        console.log("66666")
      }
      })
  },
  /**
   * 点击方法
   */
  gotoAnswer(e){
    let answer = e.currentTarget.dataset.answer;
    let id = e.currentTarget.dataset.id;
    let type= e.currentTarget.dataset.type
    console.log(e)
    if(answer == 1){
      let selectId = id;
      let url="";
      if(selectId == 1){
        url="../component/pages/smyt/smyt"
      }else if(selectId == 2){
        url="../component/pages/hxjzg/hxjzg"
      }else if(selectId == 3){
        url="../component/pages/mdjzg/mdjzg"
      }else if(selectId == 4){
        url="../component/pages/4p/4p"
      }else if(selectId == 5){
        url="../component/pages/VAKD/VAKD"
      }else if(selectId == 6){
        url="../component/pages/holland/holland"
      }else if(selectId == 7){
        url="../component/pages/MBTI/MBTI"
      }
        wx.navigateTo({
          url: url
        })
    }else{
      wx.navigateTo({
        url: '/pages/selection/index?selectId=' + id+"&type=" + type
      })
    }
  },
  /**
   * 支付完成后点击方法
   */
  gotoAnswerWxPay(){
    let answer = this.data.answer;
    let id = this.data.id;
    let type= this.data.type
    if(answer == 1){
      wx.navigateTo({
        url: '/pages/answer/index?selectId='+id
      })
    }else{
      wx.navigateTo({
        url: '/pages/selection/index?selectId=' + id+"&type=" + type
      })
    }
  }
})